const express = require("express");
let router = express.Router();

const DB = require("../utils/db完整.js");
const {api_url} = require("../config/config.js")
const {verifyToken} = require("../utils/token")
const timeStamp = require("time-stamp");



router.use((req,res,next)=>{
    //接收验证token
    console.log(req.headers.authorization,011);
    console.log(verifyToken(req.headers.authorization),111);
    if(!req.headers.authorization){
        next("请先登录");
        return
    }

    if(!verifyToken(req.headers.authorization)){
        next("请重新登录");
        return
    }
    
    next();
})
//报名接口
router.post("/study",async (req,res,next)=>{
    // let mid = "b400ec0d-9040-4ab6-813f-5c22b75d4fd6"
    //1.判断是否登录，登录了才有权限去报名
    let {mid} = verifyToken(req.headers.authorization);
    //2.有没有报名过(拿cid,mid与订单表对比，如果找到了，则报名过)
    let {cid} = req.body;
    let sql = `select * from e_order where cid="${cid}" and mid="${mid}"`;
    let [err,result] = await DB(sql);
    if(err){
        next("报名出错")
        return
    }
    //以下打印的result如果为空数组那么表示该课程还没报名过，如果有值那么表示该课程已报名
    // console.log(result);
    if(result.length>0){
        next("该课程已报名");
        return
    }


    //执行报名
    // console.log(timeStamp("YYYYMMDDHHmmssms"));
    let oid = timeStamp("YYYYMMDDHHmmssms")+(Math.random()+"").substring(2,5);
    let sql1 = `insert into e_order (oid,cid,mid,createdate) values ("${oid}","${cid}","${mid}","${Date.now()}")`;
    let [err1,result1]=await DB(sql1)
    if(err1){
        next("报名失败")
        return
    }
    
    res.send({
        status:200,
        msg:"报名成功",
        result:"订单号："+oid 
    })
    
})


//视频接口
router.get("/videoplay",async (req,res,next)=>{
    console.log(verifyToken(req.headers.authorization));
    //1.判断是否登录，登录了才有权限去判断是否可以播放
    let {mid} = verifyToken(req.headers.authorization);
    //2.有没有报名过(拿cid,mid与订单表对比，如果找到了，则报名过)
    let {id,cid} = req.query;
    let sql = `select * from e_order where cid="${cid}" and mid="${mid}"`;
    let [err,result] = await DB(sql);
    if(err){
        next("视频播放出错");
        return
    }
    console.log(err);
    console.log(result);
    //result为空那表示还没报名过
    if(result.length == 0){
        next("请先报名");
        return
    }

    //3.能走到这里，说明已经报名完成，可以请求地址
    //请求当前视频地址id
    let sql1 = `select id,cid,concat("${api_url}","/uploads/","videoPlay/",video_src) as video_src,video_title,vnum from e_video where id=${id}`;
    let [err1,result1] = await DB(sql1);
    if(err1){
        next("视频播放失败");
        return
    }
    console.log(result1[0]);

     // 3.2 把此课程的所有视频的所有信息也响应出来
     let sql2=`SELECT id,cid,concat("${api_url}","/uploads/","videoPlay/",video_src) as video_src,video_title,vnum  FROM e_video WHERE cid="${cid}"`
     let [err2,result2]=await DB(sql2)
     if(err2){
         next("视频列表请求失败")
         return
     }
     
 
     res.send({
         status:200,
         msg:"视频请求成功",
         result:{
             video:result1[0],
             videos:result2
         }
     })

})

















module.exports = router;